package cn.tedu.fruitshop.service.impl;

import cn.tedu.fruitshop.ex.ServiceException;
import cn.tedu.fruitshop.mapper.CommentMapper;
import cn.tedu.fruitshop.pojo.dto.CommentAddNewDTO;
import cn.tedu.fruitshop.pojo.dto.CommentUpdateDTO;
import cn.tedu.fruitshop.pojo.entity.Comment;
import cn.tedu.fruitshop.pojo.entity.Comment;
import cn.tedu.fruitshop.pojo.vo.CommentListItemVO;
import cn.tedu.fruitshop.pojo.vo.CommentStandardVO;
import cn.tedu.fruitshop.pojo.vo.CommentStandardVO;
import cn.tedu.fruitshop.service.ICommentService;
import cn.tedu.fruitshop.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CommentServiceImpl implements ICommentService {
    @Autowired
    CommentMapper commentMapper;

    @Override
    public void addNew(CommentAddNewDTO commentAddNewDTO) {
        log.debug("开始处理【添加评论】的业务，参数：{}", commentAddNewDTO);
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddNewDTO, comment);
        log.debug("即将插入评论数据：{}", comment);
        int rows = commentMapper.insert(comment);
        if (rows != 1) {
            String message = "服务器炸了,添加数据失败";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("插入评论数据完成");
    }

    @Override
    public void delete(Integer id) {
        CommentStandardVO queryResult = commentMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "删除评论失败,尝试访问的数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        int rows = commentMapper.deleteById(id);
        if (rows != 1) {
            String message = "服务器炸了,删除数据失败";
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public List<CommentListItemVO> list(Integer itemId) {
        return commentMapper.list(itemId);
    }
    public void updateInfoById(Integer id, CommentUpdateDTO CommentUpdateDTO){
        log.debug("开始处理【修改评论详情】的业务，参数ID：{}, 新数据：{}", id, CommentUpdateDTO);

        // 调用Mapper对象的getDetailsById()方法执行查询
        CommentStandardVO queryResult = commentMapper.getStandardById(id);
        // 判断查询结果是否为null
        if (queryResult == null) {
            // 是：此id对应的数据不存在，则抛出异常(ERR_NOT_FOUND)
            String message = "修改评论详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        Comment Comment = new Comment();
        BeanUtils.copyProperties(CommentUpdateDTO, Comment);
        Comment.setId(id);

        // 修改数据
        log.debug("即将修改数据：{}", Comment);
        int rows = commentMapper.update(Comment);
        if (rows != 1) {
            String message = "修改评论详情失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }
}
